<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

/**
 * Description of GameConfController
 *
 * @author SoChishun
 */
class ConfController extends BaseController {

    //put your code here
    public function indexAction() {
        $data = \app\index\model\ConfModel::get(1);
        return view('', ['data' => $data]);
    }

    public function doSaveAction() {
        $rule = [];
        $message = [
            'currency_rate.token' => '表单来源无效',
            'currency_rate.require' => '兑换比率未填写',
            'currency_rate.number' => '兑换比率无效'
        ];
        $fields = ['da' => '大', 'xiao' => '小', 'dan' => '单', 'shuang' => '双'];
        $rule['currency_rate'] = 'require|number|token';
        $onlyfields = [];
        $name = '';
        foreach ($fields as $key => $value) {
            $name = 'return_rate_' . $key;
            $onlyfields[] = $name;
            $rule[$name] = 'require|number';
            $message[$name . '.require'] = $value . '数返还倍率未填写';
            $message[$name . '.number'] = $value . '数返还倍率无效';
        }
        $request = \think\Request::instance();
        $request->only($onlyfields);
        $data = $request->post();
        $validate = \think\Validate::make($rule, $message);
        if (!$validate->check($data)) {
            $error = $validate->getError();
            $this->error($error);
        }

        unset($data['__token__']);
        $gameconf = new \app\index\model\ConfModel();
        if (false === $gameconf->save($data, ['id' => 1])) {
            var_export($gameconf->getError());
            $this->error($gameconf->getError());
        }
        $this->success('保存成功!', url('index'));
    }

}
